package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.ProgramaService;
import co.gov.medellin.ssti.sub.model.service.SubprogramaService;
import co.gov.medellin.ssti.sub.model.service.UsuarioSubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.AreaVO;
import co.gov.medellin.ssti.sub.model.vo.ProgramaVO;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class SelectionPermissionReportAction extends Action {
	@Autowired
	ProgramaService programaServiceImpl;
	@Autowired
	SubprogramaService subprogramaServiceImpl;
	@Autowired
	UsuarioSubprogramaService usuarioSubprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		HttpSession session = request.getSession();
		UserVO user = (UserVO) session.getAttribute("userInfo");

		String strprog = request.getParameter("codePrograma");
		String strarea = request.getParameter("codeArea");
		List<AreaVO> lSecretarias = new LinkedList<AreaVO>();
		List<ProgramaVO> lProgramas = new LinkedList<ProgramaVO>();
		List<SubprogramaVO> lSubprog = new LinkedList<SubprogramaVO>();
		List<String> lUsuSeleccion = new LinkedList<String>();
		// Proceso para guardar los usuarios seleccionados
		List lUsuarios = new LinkedList();

		lUsuarios = (List) session.getAttribute("ListaUsuarios");
		for (int i = 0; i < lUsuarios.size(); i++) {
			String seleccion = "codeusuario" + i;
			Object ocoduser = request.getParameter(seleccion);
			if (ocoduser != null) {
				lUsuSeleccion.add((String) ocoduser);
			}
		}
		// Fin del proceso
		if (lUsuSeleccion.size() > 0)
			session.setAttribute("ListadeUsuarios", lUsuSeleccion);
		try {

			lSecretarias = (List<AreaVO>) usuarioSubprogramaServiceImpl
					.findSecretaria();

			if (lSecretarias.size() > 0) {
				request.setAttribute("secretarias", lSecretarias);
			} else {
				request.setAttribute("msg", "No hay secretarias inscritas.");
			}
			if ((Object) strarea != null && !strarea.equals("nulo")) {

				ProgramaVO objProgramaVO = new ProgramaVO();
				objProgramaVO.setCodeArea(new Long(strarea));
				lProgramas = (List<ProgramaVO>) programaServiceImpl
						.findProgramaByCriteria(objProgramaVO,
								user.getUserGroupName(), user.getCode(),
								user.getCodeArea());

				if (lProgramas.size() > 0) {
					request.setAttribute("programas", lProgramas);
				} else {
					request.setAttribute("msg",
							"La Secretaria no posee programas inscritos.");
				}

				if ((Object) strprog != null && !strprog.equals("nulo")) {

					SubprogramaVO objSubProgramaVO = new SubprogramaVO();
					objSubProgramaVO.setCodePrograma(new Long(strprog));
					lSubprog = (List<SubprogramaVO>) subprogramaServiceImpl
							.findSubprogramaByCriteria(objSubProgramaVO,
									user.getCode(), user.getUserGroupName(),
									user.getCodeArea());

					if (lSubprog.size() > 0) {
						request.setAttribute("subprogramas", lSubprog);
						int tamanosubpro = lSubprog.size();
						session.setAttribute("TamanoSubpro",
								String.valueOf(tamanosubpro));
					} else {
						request.setAttribute("msg",
								"El programa no tiene beneficios inscritos.");
					}
				}
			}
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}

		return mapping.findForward("success");
	}
}